package com.shaokel.eduservice.controller;

import com.shaokel.commonutils.ReturnResult;
import com.shaokel.eduservice.entity.EduChapter;
import com.shaokel.eduservice.entity.chapter.ChapterVo;
import com.shaokel.eduservice.service.EduChapterService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 课程 前端控制器   章节
 *
 * @author 少丶客
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    //课程大纲列表，根据课程id进行查询
    @GetMapping("/getChapterVideo/{courseId}")
    public ReturnResult getChapterVideo(@PathVariable("courseId") String courseId){
        List<ChapterVo> allChapterVideo =  eduChapterService.getChapterVideoBycourseId(courseId);
        return ReturnResult.ok().data("allChapterVideo",allChapterVideo);
    }

    //添加章节
    @PostMapping("/addChapter")
    public ReturnResult addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return ReturnResult.ok();
    }

    //根据章节id查询
        @GetMapping("/getChapterInfo/{courseId}")
    public ReturnResult getChapterInfo(@PathVariable String courseId){
        EduChapter chapter = eduChapterService.getById(courseId);
        return ReturnResult.ok().data("chapter",chapter);
    }

    //修改章节
    @PostMapping("/updateChapter")
    public ReturnResult updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return ReturnResult.ok();
    }

    //删除章节
    @DeleteMapping("/{courseId}")
    public ReturnResult deleteChapter(@PathVariable String courseId){
        Boolean flag = eduChapterService.deleteChapter(courseId);
        if(flag){
            return ReturnResult.ok();
        }else{
            return ReturnResult.error();
        }
    }

}

